import server from '@/utils/http';
import { LoginParams, UpdatePass } from '@/types/user';
import { PaginationQuery } from '@/types/table';
import { encrypt } from '@/utils';
import qs from 'qs';
enum Api {
    login = '/auth/login',
    logout = '/auth/logout',
    getCode = '/auth/code',
    getUsers = '/api/users',
    getInfo = '/auth/info',
    getUserDownload = '/api/users/download',
    avatar = '/api/users/updateAvatar',
    userCenter = '/api/users/center',
    confirmPass = '/api/users/confirmPass',
    usersType = '/api/users/type',
    updatePass = '/api/users/updatePass',
}

/**
 * @description: 获取用户信息
 */
export function getUserInfo() {
    return server.Get(Api.getInfo);
}

/**
 * @description: 用户登录
 */
export const login = (params: LoginParams) => {
    const data = {
        ...params,
        password: encrypt(params.password),
    };
    return server.Post(Api.login, data);
};
/**
 * @description: 重置密码
 */
export const updatePass = (params: UpdatePass) => {
    const data = {
        ...params,
        newPass: encrypt(params.newPass),
        oldPass: encrypt(params.oldPass),
    };
    return server.Post(Api.updatePass, data);
};
/**
 *
 * @description 获取用户登录验证码
 * @returns
 */
export const getCode = () => {
    return server.Get(Api.getCode);
};

/**
 * @description: 用户修改密码
 */
function changePassword(params: LoginParams) {
    return server.Post(Api.login, params);
}

// /**
//  * @description: 用户登出
//  */
// export function logout() {
//     return server.Post(Api.logout);
// }
/**
 * @description: 用户登出
 */
export function delLogin() {
    return server.Delete(Api.logout);
}

/**
 * @description 获取用户列表
 * @param page 页码
 * @param size 分页数量
 * @returns { content: 列表 totalElements: 总数}
 */
export const getUsers = (params: PaginationQuery) => {
    return server.Get(Api.getUsers, params);
};

/**
 * @description 添加用户
 * @param user 用户信息
 */
export const addUser = (params: NormalObj): Promise<void> => {
    return server.Post(Api.getUsers, params);
};
/**
 * @description 编辑用户
 * @param user 用户信息
 */
export const editUser = (params: NormalObj): Promise<void> => {
    return server.Put(Api.getUsers, params);
};
/**
 * @description 删除用户
 * @param ids 用户id
 */
export const deleteUser = (params: (string | number)[]): Promise<void> => {
    return server.Delete(Api.getUsers, { data: params });
};

/**
 * @description 添加用户
 * @param user 用户信息
 */
export const UserDownload = (name: string) => {
    console.log(name);
    server.Download(Api.getUserDownload, { data: '' }, { responseType: 'blob' }, name);
};
export const addAvatar = (params: NormalObj): Promise<any> => {
    return server.Post(Api.avatar, params, { headers: { 'Content-Type': 'multipart/form-data' } });
};

export const editUserCenter = (params: NormalObj): Promise<any> => {
    return server.Put(Api.userCenter, params);
};

/**
 * @description 确认密码
 * @param params info
 */
export const postConfirmPass = (params: NormalObj): Promise<void> => {
    return server.Post(
        Api.confirmPass,
        qs.stringify({
            password: params.password,
        }),
        { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
    );
};
/**
 * @description 查询当前用户类型
 * @param params info
 */
export const getUsersType = (): Promise<void> => {
    return server.Get(Api.usersType);
};
